package com.neusoft.healthcity.config.scheduled;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neusoft.healthcity.config.ReadJsonConfig;
import com.neusoft.healthcity.config.utils.BeanUtil;
import com.neusoft.healthcity.entity.DeathRegInfoReback;
import com.neusoft.healthcity.entity.jsonDeath.CauseIcdOtherinfoList;
import com.neusoft.healthcity.entity.jsonDeath.CauseOtherinfoList;
import com.neusoft.healthcity.entity.jsonDeath.DeathRegList;
import com.neusoft.healthcity.service.DeathRegInfoRebackService;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;

@Component
public class JsonCeneratorDeath {

    @Autowired
    DeathRegInfoRebackService deathRegInfoRebackService;

    @Value("${jsonRead.death.jsonZipLocation}")
    private String zipPath;
    @Value("${jsonRead.death.jsonFileDescLocation}")
    private String descDir;

    private Logger logger = LoggerFactory.getLogger(JsonCeneratorDeath.class);

    long startTime = 0;
    long endTime = 0;

    @Scheduled(cron = "0 15 0 * * ?")
    public void jsonGeneratorDeathScheduled() {
        logger.info("*************死亡人口返回数据获取定时任务开始*************");
        try {
            startTime = new Date().getTime();
            logger.info("*************"+startTime+"*************");
            logger.info("*************"+endTime+"*************");
            ReadJsonConfig.unZipFiles(zipPath, descDir, startTime, endTime);
            ArrayList<File> fileArrayList = new ArrayList<>();
            ArrayList<File> files = ReadJsonConfig.getAllFile(descDir, fileArrayList);
            for (int i = 0; i < files.size(); i++) {
                File file = files.get(i);
                String input = FileUtils.readFileToString(file, "UTF-8");
                JSONObject jsonObject = JSON.parseObject(input);


                JSONObject jsonArray2 = jsonObject.getJSONObject("DataInfo");
                DeathRegInfoReback deathRegInfoReback = new DeathRegInfoReback();

                deathRegInfoReback.setCreatetime(new Date());
                deathRegInfoReback.setBusinessactivityidentification((jsonArray2.getString("BusinessActivityIdentification")));
                deathRegInfoReback.setTotalrecords(jsonArray2.getString("TotalRecords"));
                deathRegInfoReback.setStarttime(jsonArray2.getString("StartTime"));
                deathRegInfoReback.setEndtime(jsonArray2.getString("EndTime"));

                JSONArray array = jsonObject.getJSONArray("DeathRegList");
                for (int j = 0; j < array.size(); j++) {
                    JSONObject object = (JSONObject) array.get(j);
                    DeathRegList deathRegList = JSONObject.toJavaObject(object, DeathRegList.class);
                    BeanUtils.copyProperties(deathRegList, deathRegInfoReback);
                    JSONArray causeOtherinfoList = object.getJSONArray("CauseOtherinfoList");
                    for (int k = 0; k < causeOtherinfoList.size(); k++) {
                        JSONObject object1 = (JSONObject) causeOtherinfoList.get(k);
                        CauseOtherinfoList causeOtherinfoList1 = JSONObject.toJavaObject(object1, CauseOtherinfoList.class);
                        BeanUtil.copyPropertiesIgnoreCase(causeOtherinfoList1, deathRegInfoReback);
                    }
                    JSONArray arrray2 = object.getJSONArray("CauseIcdOtherinfoList");
                    for (int k = 0; k < arrray2.size(); k++) {
                        JSONObject object2 = (JSONObject) arrray2.get(k);
                        CauseIcdOtherinfoList otherinfoList = JSONObject.toJavaObject(object2, CauseIcdOtherinfoList.class);
                        BeanUtil.copyPropertiesIgnoreCase(otherinfoList, deathRegInfoReback);
                    }

                }

                //反射
                Class<? extends DeathRegInfoReback> aClass = deathRegInfoReback.getClass();
                Field[] declaredFields = aClass.getDeclaredFields();
                for (int j = 0; j < declaredFields.length; j++) {
                    Field field = declaredFields[j];
                    field.setAccessible(true);
                    Object fieldVal = field.get(deathRegInfoReback);
                    if (!"".equals(fieldVal) && null != fieldVal) {
                        String s = fieldVal.toString();
                        if (s.contains("{") || s.contains("}")) {
                            Object str = s.replace("{", "").replace("}", "");
                            field.set(deathRegInfoReback, str);
                        }
                    }
                }

                //保存返回的数据
                deathRegInfoRebackService.save(deathRegInfoReback);

            }
            endTime = startTime;
            //删除解压目录
            File file = new File(descDir);
            ReadJsonConfig.deleteDir(file);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
